﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace KstopaCommon
{
    public partial class UserLogInForm : Form
    {
        public UserType UserName;

        string _password = "";

        Dictionary<UserType, string> dict = null;

        public UserLogInForm(Dictionary<UserType, string> dict_name_password)
        {
            InitializeComponent();
            if (dict_name_password == null)
            {
                dict_name_password = new Dictionary<UserType, string>();
            }
            else
            {
                dict = dict_name_password;
            }
            string[] types = Enum.GetNames(typeof(UserType));
            foreach (var item in types)
            {
                if (item == UserType.None.ToString())
                {
                    //cmb_User.Items.Add(" ");
                    continue;
                }
                cmb_User.Items.Add(item);
            }
        }

        private void UserLogInForm_Load(object sender, EventArgs e)
        {
            //cmb_User.SelectedIndex = (int)UserType.操作员;
            cmb_User.SelectedIndex = (int)UserType.操作员 - 1;
        }

        private void cmb_User_SelectedIndexChanged(object sender, EventArgs e)
        {
            //UserName = (UserType)cmb_User.SelectedIndex;
            UserName = (UserType)(cmb_User.SelectedIndex + 1);
        }

        private void tb_Password_TextChanged(object sender, EventArgs e)
        {
            try
            {
                _password = tb_Password.Text.Trim();
            }
            catch
            {
            }
        }

        private void btn_Canel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btn_ConfirmOk_Click(object sender, EventArgs e)
        {
            if (UserName == UserType.None || UserName.ToString() == "")
            {
                MessageBox.Show("请选择用户","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                return;
            }
            if (_password == "")
            {
                MessageBox.Show("请输入密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            string password;
            dict.TryGetValue(UserName, out password);
            if (password == null || password == "")
            {
                MessageBox.Show("密码查询异常", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (password == _password)
            {
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show("请输入正确密码", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

    }
}
